package com.ityj.yygh.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ityj.yygh.hospital.service.HospitalSetService;
import com.ityj.yygh.model.hosp.HospitalSet;
import com.ityj.yygh.result.Result;
import com.ityj.yygh.util.MD5;
import com.ityj.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(value = "医院设置管理")
@RestController
@RequestMapping(path = "/admin/hosp/hospitalSet")
@Slf4j
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有的医院配置")
    @GetMapping(path = "/findAll")
    public Result findAllHospitalSet() {
        log.info("进入 findAllHospitalSet。。。");
        List<HospitalSet> hospitalSets = hospitalSetService.list();
        log.info("hospitalSets = " + hospitalSets);
        return Result.ok(hospitalSets);
    }

    // 逻辑删除，即将is_delete字段修改为1     http://localhost:8201/admin/hosp/hospitalSet/deleteById/1
    @ApiOperation(value = "根据id删除医院配置信息（逻辑删除）")
    @DeleteMapping(path = "/deleteById/{id}")
    public Result<String> deleteSetById(@PathVariable(name = "id") String id) {
        log.info("进入 deleteSetById。。。");
        boolean removeReslut = hospitalSetService.removeById(id);
        log.info("removeReslut = " + removeReslut);
        if (!removeReslut) {
            return Result.fail();
        }
        return Result.ok();
    }

    // 条件查询加分页
    @PostMapping(path = "/getHospSetByCondition/{currentPage}/{size}")
    public Result getHospSetByCondition(@PathVariable(value = "currentPage") Long currentPage,
                                        @PathVariable(value = "size") Long size,
                                        @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        log.info("进入 getHospSetByCondition。。。");
        if (ObjectUtils.isEmpty(currentPage) || ObjectUtils.isEmpty(size)) {
            log.info("入参不符合规范！");
            return Result.fail("入参不符合规范！");
        }
        if (hospitalSetQueryVo == null) {
            hospitalSetQueryVo = new HospitalSetQueryVo();
        }
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        Page<HospitalSet> page = new Page<>(currentPage, size);

        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(hosname)) {
            queryWrapper.like("hosname", hosname);
        }
        if (!ObjectUtils.isEmpty(hoscode)) {
            queryWrapper.eq("hoscode", hoscode);
        }

        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    // 增
    @PostMapping(path = "/addHospSet")
    public Result addHospSet(@RequestBody HospitalSet hospitalSet) {
        // 状态：1可用 0不可用
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        // save
        boolean saveResult = hospitalSetService.save(hospitalSet);
        if (!saveResult) {
            return Result.fail();
        }
        return Result.ok();
    }

    // 根据id查
    @GetMapping(path = "/getHospSetById/{id}")
    public Result getHospSetById(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // update
    @PostMapping(path = "/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean updateResult = hospitalSetService.updateById(hospitalSet);

        if (!updateResult) {
            return Result.fail();
        }
        return Result.ok();
    }

    @DeleteMapping(path = "/batchRemoveHospSets")
    public Result batchRemoveHospSets(@RequestBody List<Long> idLists) {
        boolean removeResult = hospitalSetService.removeByIds(idLists);

        if (!removeResult) {
            return Result.fail();
        }
        return Result.ok();
    }

    // 医院配置锁定与解锁
    @GetMapping(path = "/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean updateResult = hospitalSetService.updateById(hospitalSet);
        if (!updateResult) {
            return Result.fail();
        }
        return Result.ok();
    }

    // 发送签名秘钥 ： 短信发送
    @GetMapping(path = "/sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        // TODO 将医院信息发送给用户

        return Result.ok();
    }

}
